package com.base.myproject.client.HR.bcode;

import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Rpt_HotelTotal extends BaseJDBCPanel{
	
	private BaseStore<BusiObject> store = null;
	private TextField<String> employno = null;
	private BaseComboBoxForm<BusiObject> department = null;
	private DateField dfBeginLive;
	private DateField dfEndLive;
	private DateField dfBeginLeave;
	private DateField dfEndLeave;
	private Button btnQuery;
	private PagingToolBar toolbar;
	private BaseGrid<BusiObject> grid;
	private Button toExcel = new Button("导出到EXCEL");
	private String searchNo = "0";
	private String searchDep = "0";
	private String searchLiveBeginDate = "0";
	private String searchLiveEndDate = "0";
	private String searchLeaveBeginDate = "0";
	private String searchLeaveEndDate = "0";
	
	public Rpt_HotelTotal(){
		init();
	}
	
	public void init(){
		initWidget();
		registerMainStore(store);
	}
	public void complete() {
		super.complete();
		print.addListener(Events.OnClick, getListener());
		toExcel.addListener(Events.OnClick, getListener());
		getToolbarll().add(print);
		getToolbarll().add(toExcel);
	}
	public void initWidget(){
		HorizontalPanel hp = new HorizontalPanel();
		hp.setSpacing(3);
		
		employno = new TextField<String>();
		employno.setWidth(110);
		department = new BaseComboBoxForm<BusiObject>("id","name","Systeminfo","",true);
		department.setWidth(110);
		dfBeginLive = new DateField();
		dfBeginLive.setWidth(110);
		dfBeginLive.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		dfEndLive = new DateField();
		dfEndLive.setWidth(110);
		dfEndLive.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		dfBeginLeave = new DateField();
		dfBeginLeave.setWidth(110);
		dfBeginLeave.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		dfEndLeave = new DateField();
		dfEndLeave.setWidth(110);
		dfEndLeave.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		
		btnQuery = new Button("查询");
		btnQuery.addListener(Events.OnClick, getListener());
		hp.add(new Label("部门："));          hp.add(department);
		hp.add(new Label("雇员编号："));      hp.add(employno);
		hp.add(new Label("入宿日期从："));    hp.add(dfBeginLive);
		hp.add(new Label("至"));             hp.add(dfEndLive);
		hp.add(new Label("退宿日期从"));      hp.add(dfBeginLeave);
		hp.add(new Label("至"));             hp.add(dfEndLeave);
		hp.add(btnQuery);
		
		ContentPanel panel = new ContentPanel();
		panel.setHeaderVisible(false);
		Tools.setResize(panel, 0.99, 0.95);
		toolbar = new PagingToolBar(17);
		panel.add(getGrid());
		panel.setBottomComponent(toolbar);
		
		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(3);
		vp.add(hp);
		vp.add(panel);
		add(vp);
	}
	
	private BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		store.setToolBar(toolbar);
		store.initcompleted();
		return store;
	}

	private BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		
		type.addField("icode");
		type.addField("code");
		type.addField("bednum");
		type.addField("name");
		type.addField("employnum");
		type.addField("up_department");
		type.addField("systemid");
		type.addField("sex");
		type.addField("livedate");
		type.getField("livedate").setType(Date.class);
		type.addField("nativeplace");
		type.addField("cerno");
		type.addField("leavedate");
		type.getField("leavedate").setType(Date.class);
		type.addField("entrydate");
		type.getField("entrydate").setType(Date.class);
		type.addField("Resignationdate");
		type.getField("Resignationdate").setType(Date.class);

		StoreInfo info = new StoreInfo();
		info.setSql("select * from bcode where 1=2");
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	
	private BaseGrid<BusiObject> getGrid(){
		if(grid == null){
			createGrid();
		}
		return grid;
	}
	
	private BaseGrid<BusiObject> createGrid(){
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		
		ColumnConfig c1 = new ColumnConfig("code", "房号", 60);
	    ColumnConfig c2 = new ColumnConfig("bednum", "床位", 60);
	    
	    ColumnConfig c3 = new ColumnConfig("name", "姓名", 80);
	    ColumnConfig c4 = new ColumnConfig("employnum", "工号", 80);
	    ColumnConfig c5 = new ColumnConfig("up_department", "部门", 120);
	    BaseComboBoxForm<BaseModelData> C_deppraent = new BaseComboBoxForm<BaseModelData>("id","name","SystemInfo","where len(id)=4",true);
	    c5.setRenderer(C_deppraent);
	    ColumnConfig c6 = new ColumnConfig("systemid", "分部门", 120);
	    BaseComboBoxForm<BaseModelData> C_dep = new BaseComboBoxForm<BaseModelData>("id","name","SystemInfo","where name is not null and id is not null",true);
	    c6.setRenderer(C_dep);
	    ColumnConfig c7 = new ColumnConfig("sex", "性别", 60);
	    ColumnConfig c8 = new ColumnConfig("livedate", "入住日期", 100);
	    c8.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
	    ColumnConfig c9 = new ColumnConfig("nativeplace", "籍贯", 80);
	    ColumnConfig c10 = new ColumnConfig("cerno", "居住证", 80);
	    ColumnConfig c11 = new ColumnConfig("leavedate", "退宿日期", 100);
	    c11.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
	    ColumnConfig c12 = new ColumnConfig("entrydate", "入职日期", 100);
	    c12.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
	    ColumnConfig c13 = new ColumnConfig("Resignationdate", "离职日期", 100);
	    c13.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
	    
		columns.add(c1);
		columns.add(c2);
		columns.add(c3);
		columns.add(c4);
		columns.add(c5);
		columns.add(c6);
		columns.add(c7);
		columns.add(c8);
		columns.add(c9);
		columns.add(c10);
		columns.add(c11);
		columns.add(c12);
		columns.add(c13);
		ColumnModel cm = new ColumnModel(columns);
		
		grid = new BaseGrid<BusiObject>(getStore(), cm,false,false);
		grid.setBorders(false);
		grid.setLoadMask(true);
		Tools.setResize(grid, 0.99, 0.90);
		return grid;
	}
	
	int sync = 0;
	Listener<BaseEvent> defaultlistener;
	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}
		defaultlistener = new Listener<BaseEvent>() {

			public void handleEvent(BaseEvent be) {
				sync++;
			
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							
						}else if (be.getType() == Events.OnClick) {
							if(be.getSource().equals(btnQuery)){
								String where = " where 1=1";
								if(!employno.getRawValue().equals("")){
									searchNo = employno.getValue();
									where += " and employnum like '%25" + searchNo + "%25'";
								} else {
									searchNo = "0";
								}
								
								if(!department.getRawValue().equals("")){
									searchDep = department.getValueCode();
									where += " and systemid like '"+searchDep+"%25'";
								} else {
									searchDep = "0";
								}
								
								if(!dfBeginLive.getRawValue().equals("") && !dfEndLive.getRawValue().equals("")){
									searchLiveBeginDate = dfBeginLive.getRawValue();
									searchLiveEndDate = dfEndLive.getRawValue();
									where += " and livedate between '"+searchLiveBeginDate+"' and '"+searchLiveEndDate+"'";
								} else {
									searchLiveBeginDate = "0";
									searchLiveEndDate = "0";
								}
								if(!dfBeginLeave.getRawValue().equals("") && !dfEndLeave.getRawValue().equals("")){
									searchLeaveBeginDate = dfBeginLeave.getRawValue();
									searchLeaveEndDate = dfEndLeave.getRawValue();
									where += " and leavedate between '"+searchLeaveBeginDate+"' and '"+searchLeaveEndDate+"'";
								} else {
									searchLeaveBeginDate = "0";
									searchLeaveEndDate = "0";
								}
								query(where);
							} else  {
								StringBuilder data = new StringBuilder();
								data.append("dep=");
								data.append(searchDep);
								data.append("&no=");
								data.append(searchNo);
								data.append("&liveBegin=");
								data.append(searchLiveBeginDate);
								data.append("&liveEnd=");
								data.append(searchLiveEndDate);
								data.append("&leaveBegin=");
								data.append(searchLeaveBeginDate);
								data.append("&leaveEnd=");
								data.append(searchLeaveEndDate);
								
								if (be.getSource().equals(toExcel)) {
									data.append("&excel=1");
								}
								Window.open(GWT.getHostPageBaseURL() + "hr_Jsp/toHotelTotalExcel.jsp?" + data,"_blank","menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
							}
						} else if (be.getType() == Events.Change) {
							
						}
						else if(be.getType().equals(Events.RowDoubleClick))
						{	
						}else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13){ // 回车
									
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}
		};
		return defaultlistener;
	}
	
	private void query(String where){
		String sql = "select c.code as code,bednum,a.bname as name,employnum,up_department,systemid,sex,livedate,nativeplace,cerno,leavedate,entrydate, Resignationdate from " +
					 "(select bcode,bname,Identifier %2B bcode as employnum,up_department,systemid,sex,nativeplace from bcode) as a inner join " +
					 "(select bcode,bednum,livedate,leavedate,code from hr_hostel_bcode) as c on a.bcode=c.bcode left join " +
					 "(select bcode,cerno from bcode_cer where cerSpecies='暂住证') as b on a.bcode=b.bcode inner join " +
					 "(select entrydate, Resignationdate, bcode from bcode_job)as d on a.bcode=d.bcode " + where;
		
		grid.getStore().getStoreinfo().setSql(sql);
		grid.getStore().reload();
		grid.getView().refresh(true);
	}
}
